%function decompUnconditional 
clear all; close all;clc
outPath = 'C:\Users\g1mxo01\Documents\MATLAB\Alex';
outName = 'unconditionalEnd';
modelPath = 'O:\PROJ_LIB\FOMC\MACRO\DSGE\models\CEFJ\BigARMAHP\Prior9 Gap OverIdent HP FL\02 03 12';

cd(modelPath);
load workspace;
cd('O:\PROJ_LIB\LJM\exog');
%% 1. Define What the decomposition will be 

histStru.report={'GDP','Consumption','Investment','FFR','Hours','PCE Core','FFR 4q Ahead'};

histStru.groupnames={'Demand','Supply','Forward Guidance','Other Policy','Residual'};
groupStruct.g1={'Discount','Spread','Net Worth','CIV+G+NX'};
groupStruct.g2={'Neutral','ISTS','Price Markup','Labor Disutility'};
groupStruct.g3={'Factor 2'};
groupStruct.g4={'Factor 1','Inflation Drift'};

% Date to Begin and End 
startDate=1989.25;
endDate=2007.25; 

% Options for plots
plotResidual = true;
plotSameScale = true;

%% Calculate Constants and Scales

constantsReal
% Vector of Model Steady States

ssVec(1) = paramGDP; 
ssVec(2) = paramCONS;
ssVec(3) = paramINV;
ssVec(4) = paramFFR;
ssVec(5) = paramHours;
ssVec(6) = paramPCECore;
ssVec(7) = paramFFR;

% Scale to Multiply by
scale = [4 4 4 4 1 4 4];

%% 2. Solve Model and Extract Names
% [GG, RR, CONS, eu, SDX, ZZ, initss, ssvec,~, ssNames,stateNames,shockNamesStru]...
%     =feval(funcmod,param,solveopt,addsol);

%% 3. Define nameStru, Structure with the Names 

shockNames=shockNamesStru.long; 
[Znames,ZposInStates]=extractObsNames(ZZ(:,:,1),stateNames);
nameStru.stateNames=stateNames;
nameStru.shockNames=shockNames;
nameStru.obsNames=Znames;
shockNames=shockNamesStru.long; 

%% 4. Define sampleStr, Structure with all the sample information 

sampleStru.sampleVec=sample; 
sampleStru=sampleAssignments(sampleStru);
addsol.tauVec=sampleStru.tauVec;
% [KFStru,logL]=feval(addsol.funcKfilter,param,funcmod,Y,trainvec,solveopt,addsol); 

%% 5. Define Key Variables

% Position of states to report
reportPos = cellposition(histStru.report,stateNames);

% Position of starting point in sample
startPos = find(sampleStru.sampleVec == startDate);

% Position of end point of sample
endPos   = find(sampleStru.sampleVec == endDate);

% Structure holding positions and names of groups
gsum = zb_extractgroups(groupStruct,shockNames,histStru.groupnames); 
nGroups = length(histStru.groupnames); 
nReportSt = length(reportPos);

%% 6. Calculuate Group Contributions

countMatGroup = zeros(length(sample), nReportSt, nGroups);

% Sum over shocks in each group for each state
for k = 1:nGroups
    
    shockInx = gsum.(['n',num2str(k)]);
    countMatGroup(:,:,k) = sum(KFStru.countSt(:, reportPos, shockInx), 3);
    
end
%% 7. Plot Settings

% Plot Settings
plotStruct = plotAssignDefaults([]);
plotStruct.lineStyles = {'-','-'};
subSample = sample(startPos:endPos);
plotStruct.xtickValues = subSample(1:8:end);
plotStruct.xtickLabels = sampleSS.sampleCell(startPos:8:endPos);

% Whether to plot "Residual" category
if ~plotResidual
    nGroups = nGroups - 1;
end

% Plot Titles
plotTitle = {'GDP (per capita)', 'Consumption (per capita)', 'Investment (per capita)',...
             'Federal Funds Rate', 'Hours (per capita)', 'PCE Core','FFR 4q Ahead'};

%% 8. Construct Plots

gvec = zeros(nReportSt,1);
close all;

for k = 1:nReportSt
    
    gvec(k) = figure;
    
    % If we are plotting hours, change y-axis label
    if strcmp(histStru.report(k),'Hours')
        display('Scaling Hrs to 1');
        plotStruct.yLabel = '';
    else
        plotStruct.yLabel = 'Annualized';
    end
    
    % Plot Data
    subplot(nGroups+1,1,1)
    
    plotMat = scale(k)*(KFStru.smoothSt(startPos:endPos, reportPos(k)) + ssVec(k));
    plotStruct.legend = {'Data'};
    plotStruct.colors = [.5 .5 .5];
    plot3sampSub(subSample,plotMat, 'Data', 1, plotStruct);
    h = hline(scale(k)*ssVec(k));
    set(h,'linewidth',2)
    legend off
    
    % Counter factuals
    %yBounds = limsaj(scale(k)*countMatGroup(startPos:endPos,k,1:nGroups),0);
    
    tempYMin=min(min( scale(k)*countMatGroup(startPos:endPos,k,2:nGroups) ))-0.2;
    tempYMax=max(max( scale(k)*countMatGroup(startPos:endPos,k,2:nGroups) ))+0.2;
    
    %plotStruct= rmfield(plotStruct,'ylim'); 

    for m = 1:nGroups

       subplot(nGroups+1,1,m+1)
       plotMat = scale(k)*countMatGroup(startPos:endPos,k,m);
       plotStruct.legend = {'Counter Factual'};
       plotStruct.colors = [0 0 0];
       
       if plotSameScale
            if m > 1 
                plotStruct.ylim=[tempYMin tempYMax]; 
            end 
       end

       [h1 h2 h3] = plot3sampSub(subSample,plotMat, histStru.groupnames(m), 1, plotStruct);  
       
       %if plotSameScale
        %   set(h3,'Ylim',yBounds);
       %end
       hand=hline(0); 
       set(hand,'Color',[1 0 0],'LineStyle','--');  
       %keyboard
    end
    
    plotStruct.ylim = [];
    suptitle(['                       ',plotTitle(k)], 15);
    set(gvec(k),'paperPosition', [0.25 0.1 8.0 10.50]);
    
end
keyboard
% plotMat = 4*KFStru.innovations(startPos:endPos,11);
% plot3sampSub(subSample,plotMat, 'Factor 2', 1, plotStruct)


cd(outPath);
plots2PDF(gvec,'uncondFFR4Q');
% close all
